<?php
require_once("../includes/config.inc.php");
validate_admin();
$id = $_REQUEST['id'];
if($_POST)
{
	@extract($_POST);
	$address=mysql_real_escape_string($address);

	// Edit Code
	$noerror=true;
	if(isFieldDuplicate($tableName='tbl_address_book',$fieldName='email',$filedValue=$email,$autoIdName='id',$autoIdValue=$id,$isNullAllowed=false)) 
	{	
		$_SESSION['sessMsg'] = " Sorry, this Email ".$email." already exists. Please choose a different Email . ";
		$noerror=false;
	}
	if($noerror)
	{	
		if($id)
		{
			$sqlEdit = " update tbl_address_book set
			category = '$category',
			organizationType = '$organizationType',
			organizationName = '$organizationName',
			firstName = '$firstName',
			middleName = '$middleName',
			lastName = '$lastName',
			designation = '$designation',
			address = '$address',
			address2 = '$address2',
			city = '$city',
			state = '$state',
			country = '$country',
			zipCode = '$zipCode',
			phone = '$phone',
			phone1 = '$phone1',
			phone2 = '$phone2',
			phone3 = '$phone3',
			phone4 = '$phone4',
			website = '$website',
			fax = '$fax',
			email = '$email',
			secondaryEmail = '$secondaryEmail',
			remarks = '$remarks'
			where id ='$id'";
			executeQuery($sqlEdit);
			
			$_SESSION['sessMsg'] = "Detail Edited Successfully. ";
			header("location:addressBookManager.php");
			exit();
		}
		else
		{
			// Add Code
			$sqlPA = " insert into tbl_address_book 
			(
				category,
				organizationType,
				organizationName,
				firstName,
				middleName,
				lastName,
				designation,
				address,
				address2,
				city,
				state,
				country,
				zipCode,
				phone,
				phone1,
				phone2,
				phone3,
				phone4,
				email,
				secondaryEmail,
				fax,
				website,				
				remarks,
				regDate,
				status
			)
			values
			(
				'$category',
				'$organizationType',
				'$organizationName',
				'$firstName',
				'$middleName',
				'$lastName',
				'$designation',
				'$address',
				'$address2',
				'$city',
				'$state',
				'$country',
				'$zipCode',
				'$phone',
				'$phone1',
				'$phone2',
				'$phone3',
				'$phone4',
				'$email',
				'$secondaryEmail',
				'$fax',
				'$website',
				'$remarks',
				CURDATE(),
				'1'
			)
			";
			executeQuery($sqlPA );			
			$_SESSION['sessMsg'] = " New Record Added Successfully. ";
			header("location: addressBookManager.php");
			exit();
		}
	}		
}
			
if($id)
{
	$sqlPD = " select * from tbl_address_book where id = '$id' ";
	$resPD = executeQuery($sqlPD);
	if($rowPD = mysql_fetch_array($resPD))
	{
		@extract($rowPD);
	}
}
?>